<?php
	//$tableWriter = new CmsTableWriter(CMS_FORMAT,$db);
	$cmsDocument = new CmsDocument($db);
	//$cmsDocument->flush();
	$userTable = new CmsTableWriter("user", $db);
	$tableWriter = new CmsTableWriter("content", $db);
	$tableWriter->resetCustomIterator("Where documenttypecode='docPerson' and authorbio='".$authInfo->username."' ");
	$person = $tableWriter->nextItem();
	$personCode = $person->documentcode;
	$error = true;
	
	$submit = extractStringWithKeyFromArray("submit", $_REQUEST);
	$pass = extractStringWithKeyFromArray("pass", $_REQUEST);
	$repeat = extractStringWithKeyFromArray("repeat", $_REQUEST);
	if(isset($submit) && $submit == true) {
		if((isset($pass) && isset($repeat)) && ($pass != '' && $repeat != '') && $repeat == $pass) {
			$userTable->begin();
			$userTable->addField("uid", $authInfo->uid);
			$userTable->addField("usertype", $authInfo->usertype);
			$userTable->addField("username", $authInfo->username);
			$userTable->addField("password", MD5($repeat));
			$error = $userTable->saveItem();
			if($error)
				$authInfo->password = md5($repeat);
				$password = $repeat;
		} else {
			$error = false;
			$errorMessage = "Make sure you enter your password twice and that it is exactly the same.";
		}
	}
?>
<div class="cms_browse_heading">
	<div class="cms_browse_heading_label">
		<h1>Change Password</h1>
	</div>
	<div class="cms_browse_addrecord"></div>
</div>	
<div id="cms_browse_topdivider" class="cms_browse_divider"></div>
<div id="cms_browse">
<?
				if(isset($submit) && $submit != '' && $error) {
?>
					<table border="0" cellpadding="0" cellspacing="0" class="cms_browsetext">
		              <tr>
		              	<td>Your Password has been changed!</td>
		              </tr>
		            </table>
					
<?
				} else {
?>
              <form name="changeForm" id="changeForm" method="post" enctype="multipart/form-data" action="index.php?fileNo=14">
              <input type="hidden" name="submit" value="true">
              <table border="0" cellpadding="0" cellspacing="0" width="100%" class="cms_browsetext">
<?
					if(!($error)) {
						echo "<tr><td colspan=\"2\"><span class=\"body2-8\">$errorMessage</span><br><br></td></tr>";
					}
?>
              <tr>
              	<td >New Password:</td>
              	<td >&nbsp;<input type="password" name="pass"></td>
              </tr>
              <tr>
              	<td >Repeat:</td>
              	<td >&nbsp;<input type="password" name="repeat"></td>
              </tr>
              </table>
              <input type="submit" value="Submit">
              </form>
<?
				}
?>
</div>
